<template>
    <!-- 新增弹出框 -->
    <el-dialog v-bind:title="titleName" :visible="Visible" :modal="true" width="70%" :show-close="true" :destroy-on-close="true"  :close-on-click-modal="false" :before-close="handleDialogClose">
        <el-form ref="rulesForm" :model="data"  :rules="rules">
            <el-form-item label="标题" prop="notice_title">
                <el-input v-model="data.notice_title"></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="notice_content">
                <quill-editor ref="myTextEditor" v-model="data.notice_content"></quill-editor>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="close">关 闭</el-button>
                <el-button type="primary" @click="save('rulesForm')">保 存</el-button>
            </span>
    </el-dialog>
</template>

<script>
    import ElFormItem from 'element-ui/packages/form-item';
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import { quillEditor } from 'vue-quill-editor';
 
    export default {
        name: "edit",
        components: {
            ElFormItem,
            quillEditor
        },
        inject:['refresh'],
        props: {
            titleName: {
                type: String,
                default: '新增'
            },
            Visible: {
                type:Boolean
            },
            data: {
                notice_pk:'',
                notice_content:'',
                notice_title:'',
                notice_time: '',
                notice_person: '',
                fk_user_pk: '',
            }
        },
        data(){
            return {
                url:'',
                url_save: '/auth/notice/save',
                url_edit: '/auth/notice/update',
                //在return 后面；
                rules: {
                    // 要以数组形式展示
                    title: [
                        { required: true, message: "请输入标题", trigger: "change" }
                    ],
                    content: [
                        { required: true, message: "请输入内容", trigger: "change" }
                    ],
                }
            }
        },
        created() {
            
        },
        methods:{
            //关闭窗口方法
            close(){
                // this.Visible = false;
                //通过调用父组件的方法设置Visible的值为false
                this.refresh();
            },
            //保存数据方法
            save(formName){
                /*判断是添加还是修改*/
                if (this.data.notice_pk === '' || this.data.notice_pk === undefined) {
                    this.data.notice_person = localStorage.getItem('ms_username');
                    this.data.fk_user_pk = localStorage.getItem('ms_userPk');
                    this.url = this.url_save
                }else {
                    this.url = this.url_save
                }
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        console.log("success submit!!");
                        this.$post(this.url, this.data).then(res => {
                            this.$message({
                                type : res.code,
                                message: res.msg,
                            });
                            this.Visible=false;
                            //刷新列表页的数据
                            this.refresh();
                        })
                    }else{
                        console.log("error submit!!");
                        return false;
                    }
                })
            },
            //右上角关闭事件
            handleDialogClose(){
                this.close();
            },
        }
    }
</script>